<!--
 * @Author: HeAo
 * @Date: 2021-10-13 16:24:35
 * @LastEditTime: 2021-10-13 16:54:02
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2103code\3-2-Vue\day05\7-编程式导航.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/vue.js"></script>
  <script src="../js/vue-router.js"></script>
  <style>
    header {
      height: 50px;
      background-color: teal;
    }

    main {
      display: flex;
    }

    .left {
      width: 150px;
      border: 3px solid orange;
      height: 700px;
      margin: 10px 10px 0 0;
    }

    .right {
      /* flex: 1; */
      width: calc(100% - 150px);
      border: 3px solid blue;
      height: 700px;
      margin-top: 10px;
    }
  </style>
</head>

<body>
  <div id="app">
    <header>我是网站头部</header>
    <main>
      <div class="left">
        <router-link to="/user">用户管理</router-link><br>
        <router-link to="/student">学生管理</router-link><br>
        <router-link to="/clazz">班级管理</router-link>
      </div>
      <div class="right">
        <!-- 为组件加载提供容器/窗口 -->
        <!-- 实现了组件之间的动态加载 -->
        <router-view></router-view>
      </div>
    </main>
  </div>
  <script>
    // 1.注册组件
    let com1 = {
      data() {
        return {}
      },
      template: `
        <div>
          用户管理
        </div>
      `
    }
    let com2 = {
      data() {
        return {}
      },
      template: `
        <div>
          学生管理 
          <div>
            <button @click="clickHandler">成绩管理</button>
            <button @click="clickHandler">学籍管理</button>
            <button @click="$router.go(-1)">返回</button>

          </div>
          <router-view></router-view>
        </div>
      `,
      methods: {
        clickHandler(e) {
          if (e.target.innerText == '成绩管理') {
            // 使用this.$router.push完成编程式导航
            // this.$router.push({ path: '/student/grade' })
            if (this.$route.fullPath != '/student/grade') {
              // this.$router.push({ name: 'grade' })
              // this.$router.push({
              //   name: 'grade',
              //   params: {
              //     id: 1001,
              //     name: 'zhangsan'
              //   }
              // })
              this.$router.replace('/student/grade')
            }

          } else {
            // this.$router.push({ path: '/student/register' })
            if (this.$route.fullPath != '/student/register') {
              // this.$router.push({ name: 'register' })
              // 使用path跳转，并且使用query传递参数
              this.$router.push({
                path: '/student/register',
                query: {
                  id: 1002,
                  name: 'lisi'
                }
              })
            }
          }
        }
      }
    }
    let com3 = {
      data() {
        return {}
      },
      template: `
        <div>
          班级管理 
        </div>
      `
    }
    let comGrade = {
      data() {
        return {}
      },
      template: `
        <div>
          成绩管理
        </div>
      `,
      created() {
        console.log('params', this.$route.params);
      }
    }
    let comRegister = {
      data() {
        return {}
      },
      template: `
        <div>
          学籍管理 
        </div>
      `,
      created() {
        console.log('query', this.$route.query);
      }
    }
    // 2.定义路由
    let router = new VueRouter({
      // 修改路由模式
      mode: 'history',
      routes: [
        { path: '/user', component: com1 },
        {
          path: '/student',
          component: com2,
          children: [
            { path: 'grade', component: comGrade, name: 'grade' },
            { path: 'register', component: comRegister, name: 'register' },
          ]
        },
        { path: '/clazz', component: com3 }
      ]
    })

    let vm = new Vue({
      el: '#app',
      data: {},
      methods: {},
      // 3.路由注册
      router: router
    })
  </script>
</body>

</html>